package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.List;


/**
 * 
 * @author David Spruill 
 *       edited by Wynne Lok
 * 
 */

public class FrequencyPlot extends Axis {

    //private Color myLineColor;
    //private Dimension mySize;
    private int myOffset;
    private int myGraphWidth;
    private int myGraphHeight;
    private int myDataMax;
    //private int myDataMaxX;
    private int myDataSize;
    private Color myPointColor;
    private int myPointSize;

    /**
     * Takes information from graph and sets the data to be used in Line graph.
     * 
     * @param screenSize is the size of the display
     * @param graphOffset is how much away from the edges of the screen the
     *        graph is
     */
    public FrequencyPlot (Dimension screenSize, int graphOffset) {
        setSize(screenSize);
        setOffset(graphOffset);
        setGraphWidth(screenSize.width - 2 * graphOffset);
        setGraphHeight(screenSize.height - 2 * graphOffset);
    }

    /**
     * Sets the color of the line in the line graph.
     */
    public void setDefaults () {
        super.setDefaults();
        //myLineColor = Color.orange;
        //mySize = getMySize();
        myOffset = getOffset();
        myGraphWidth = getGraphWidth();
        myGraphHeight = getGraphHeight();
        myDataMax = getDataMax();
        myDataSize = getMyDataSize();
        myPointColor = getMyPointColor();
        myPointSize = getMyPointSize();
        //myDataMaxX = getMyDataMaxX();
        // point graph/line-graph specific defaults
    }

    /**
     * Draws the lines between points.
     * 
     * @param g the graphics object with which to draw
     * @param data the list of data to graph
     */
    @Override
    public void paint (Graphics g, List<Integer> data) {
        setDataMax(getMax(data));
        setDataSize(data.size());
        myDataMax = getDataMax();
        myDataSize = getMyDataSize();
        drawAxis(g);
        drawPoints(g, myPointColor, data, myPointSize);
        //drawLines(g, data);

    }
    /**
    @Override
    public void paint (Graphics g, List<Integer> xData, List<Integer> yData) {
        setMyDataMax(getMax(yData));
        myDataMax = getMyDataMax();
        setMyDataMaxX(getMax(xData));
        myDataMaxX = getMyDataMaxX();
        setMyDataSize(xData.size());
        myDataSize = getMyDataSize();
        drawAxis(g);
        drawPoints(g, myPointColor, xData, yData, myPointSize);
        drawLines(g, xData, yData);

    }
    */

    /**
     * This draws points to the graph inside of the axis
     * 
     * @param g the graphics object used to draw the points
     * @param pointColor the color with which to draw the points
     * @param data the actual column of data to draw as points (a list of
     *        numbers stored as strings)
     */
    private void drawPoints (Graphics g, Color pointColor, List<Integer> data, int pointSize) {
        float xStep = myGraphWidth / (float) myDataSize;
        double xPos = myOffset;
        g.setColor(pointColor);
        for (Integer number : data) {
            int yPos =
                    (int) ((float) ((myDataMax - number) /
                            (float) myDataMax) * (float) myGraphHeight);
            g.drawOval((int) (xPos - pointSize / 2), myOffset + yPos - pointSize / 2, pointSize,
                       pointSize);
            xPos += xStep;
        }
    }
/**
    private void drawPoints (Graphics g, Color pointColor, List<Integer> xData,
                             List<Integer> yData, int pointSize) {
        g.setColor(pointColor);
        if (xData.size() != yData.size()) {
            // throw exception
        }
        else {
            for (int i = 0; i < xData.size(); i++) {
                int yPos =
                        (int) ((float) ((myDataMax - yData.get(i)) /
                                (float) myDataMax) * (float) myGraphHeight);
                int xPos =
                        (int) ((float) ((xData.get(i)) /
                                (float) myDataMaxX) * (float) myGraphWidth);
                g.drawOval(myOffset + xPos - pointSize / 2, myOffset + yPos - pointSize / 2,
                           pointSize, pointSize);
            }
        }
    }

    private void drawLines (Graphics g, List<Integer> xData, List<Integer> yData) {
        int graphWidth = mySize.width - 2 * myOffset;
        int graphHeight = mySize.height - 2 * myOffset;
        int dataMax = getMax(yData);
        int dataMaxX = getMax(xData);

        g.setColor(myLineColor);
        int firstY =
                myOffset +
                        (int) ((float) ((dataMax - yData.get(0)) /
                                (float) dataMax) * (float) graphHeight);
        int firstX =
                myOffset + (int) ((float) ((xData.get(0)) / (float) dataMaxX) * (float) graphWidth);
        for (int i = 0; i < yData.size(); i++) {
            int newY =
                    myOffset +
                            (int) ((float) ((dataMax - yData.get(i)) /
                                    (float) dataMax) * (float) graphHeight);
            int newX =
                    myOffset +
                            (int) ((float) ((xData.get(i)) /
                                    (float) dataMaxX) * (float) graphWidth);

            g.drawLine(firstX, firstY, newX, newY);

            firstX = newX;
            firstY = newY;
        }

    }

    private void drawLines (Graphics g, List<Integer> data) {
        int graphWidth = mySize.width - 2 * myOffset;
        int graphHeight = mySize.height - 2 * myOffset;
        float xStep = graphWidth / (float) data.size();
        int dataMax = getMax(data);
        g.setColor(myLineColor);
        int firstY =
                myOffset +
                        (int) ((float) ((dataMax - data.get(0)) /
                                (float) dataMax) * (float) graphHeight);
        float firstX = myOffset;
        List<Integer> dataCopy = data.subList(1, data.size());
        for (Integer number : dataCopy) {
            int newY =
                    myOffset +
                            (int) ((float) ((dataMax - number) /
                                    (float) dataMax) * (float) graphHeight);
            float newX = firstX + xStep;

            g.drawLine((int) firstX, firstY, (int) newX, newY);

            firstX = newX;
            firstY = newY;
        }
    }
*/
}
